
#include "chesspane.h"
#include "chessboard.h"

ChessPane::ChessPane(int x, int y, ChessBoard* board, char myType): color(myType)
{
    this->board = board;
    this->x = x;
    this->y = y;
}

int ChessPane::CheckStart(int dire)
{
    // 分析出 XY轴的分量方向
    switch (dire) {
    case 1 :
        return CheckStart(-1, -1);
    case 2 :
        return CheckStart(0, -1);
    case 3 :
        return CheckStart(1, -1);
    case 4 :
        return CheckStart(-1, 0);
    case 5 :
        return CheckStart(1, 0);
    case 6 :
        return CheckStart(-1, 1);
    case 7 :
        return CheckStart(0, 1);
    case 8 :
        return CheckStart(1, 1);
    }
    return 0;
}

int ChessPane::CheckStart(int dx, int dy)
{
    return Check(dx, dy , color);
}

int ChessPane::Check(int dx, int dy, const char expectType)
{
    /* 判断棋子颜色，不同色则返回 */
    if (this->color != expectType)
        return 0;
    
    if (dx == 0 && dy == 0)
        return 0;

    int nextx = this->x + dx;
    int nexty = this->y + dy;
    /* 超出棋盘边界，返回 */
    if (nextx < 0 || (unsigned)nextx > board->paneNumber() - 1)
        return 0;
    
    if (nexty < 0 || (unsigned)nexty > board->paneNumber() - 1)
        return 0;

    /* 规定方向上下一个空格没有棋子，返回 */
    ChessPane* pane = board->paneGrid[nextx][nexty];
    if (pane == NULL)
        return 1;
    else /* 按方向查询下一粒chess */
        return (1 + pane->Check(dx, dy, expectType));
}

